#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main()
{
    int n;
    cin >> n;
    priority_queue<int, vector<int>, greater<int>> heap;
    int m;
    while (n--)
    {
        scanf("%d", &m);
        heap.push(m);
    }

    int ret = 0;
    while (heap.size() > 1)
    {
        int a = heap.top();
        heap.pop();
        int b = heap.top();
        heap.pop();
        ret += a + b;
        heap.push(a + b);
    }
    printf("%d", ret);
    return 0;
}